﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace WindowsGame4
{
    class Player_MoveState : State
    {
        private static Player_MoveState instance;

        private Player_MoveState()
        {
            base.Name = "MoveState";
        }

        public override void enter(AIObject aiObject)
        {
            //Console.WriteLine("<Player_MoveState> enter");
            AIPlayer aiPlayer = (AIPlayer)aiObject;
            Player player = (Player)aiPlayer.Player;
            player.Moving = true;
        }

        public static Player_MoveState getInstance()
        {
            if (instance == null)
                instance = new Player_MoveState();
            return instance;
        }

        public override void execute(AIObject aiObject)
        {
            // Move
            AIPlayer aiPlayer = (AIPlayer)aiObject;
            Player player = (Player)aiPlayer.Player;
            GameInfo gameInfo = GameInfo.getInstance();

            if (player.ReloadTime > 0)
                player.ReloadTime -= player.Level;
            else
                player.Attack = true;

            //System.Console.WriteLine(player.ReloadTime);
            // Check colision
            /*
            if (player.Movement != null)
            {
                gameInfo.checkItem(player);

                int dx = player.Movement.X - player.Position.X;
                int dy = player.Movement.Y - player.Position.Y;
                //System.Console.WriteLine(dx);
                if (dx == 0 && dy == 0)
                {
                    player.Movement = null;
                    player.Colision = false;
                }
                else
                {
                    if(Math.Abs(dx) < player.Speed)
                        player.Position.X += Math.Abs(dx) * Math.Sign(dx);
                    else
                        player.Position.X += player.Speed * Math.Sign(dx);
                    
                    if (Math.Abs(dy) < player.Speed)
                        player.Position.Y += Math.Abs(dy) * Math.Sign(dy);
                    else
                        player.Position.Y += player.Speed * Math.Sign(dy);
                    
                    
                }
            }

            if (player.Movement == null)
                EventEmitter.getInstance().registerMessage(0, aiPlayer.Id, aiPlayer.Id, Message.MessageType.msg_stop);*/

            Position2D previousPosition = new Position2D(player.Position.X, player.Position.Y);

            player.Position.X += player.Speed * player.Direction.X;
            player.Position.Y += player.Speed * player.Direction.Y;

            gameInfo.checkItem(player);

            // Detect colision
            ArrayList type = new ArrayList();
            type.Add("character");
           // type.Add("player");
            //type.Add("enemy");
            ArrayList ignore = new ArrayList();
            ignore.Add(player.Id);
            
            Object colision = gameInfo.checkColision(player, "character", ignore);
            if (colision != null)
            {
                player.Position.X = previousPosition.X;
                player.Position.Y = previousPosition.Y;
                //System.Console.ReadKey();
                EventEmitter.getInstance().registerMessage(0, aiPlayer.Id, aiPlayer.Id, Message.MessageType.msg_stop);
            }
            
            /*
            int rowPlayer = gameInfo.World.getNumRow(player.Position.Y);
            int columnPlayer = gameInfo.World.getNumColumn(player.Position.X);
            if(player.Direction.getDirection() == "right")
            {
                Cell newCell = gameInfo.World.get(rowPlayer, columnPlayer+1);
                if (newCell.Obj != null)
                {
                    // Colision
                    //System.Console.WriteLine("al pelo");
                    //System.Console.ReadKey();
                    EventEmitter.getInstance().registerMessage(0, aiPlayer.Id, aiPlayer.Id, Message.MessageType.msg_stop);
                }
            }
            */

            /*
                System.Console.WriteLine("objec");
            if (newCell.Obj != null && newCell.Obj.Id != player.Id)
            {


            }
            else
            {
                
                int previousRow = gameInfo.World.getNumRow(previousPosition.Y);
                int previousColumn = gameInfo.World.getNumColumn(previousPosition.X);
                if (rowPlayer != previousRow || columnPlayer != previousColumn)
                {
                    Cell cell = gameInfo.World.get(previousRow, previousColumn);
                    cell.Obj = null;
                    cell = gameInfo.World.get(rowPlayer, columnPlayer);
                    cell.Obj = player;
                }
            }
             */
            /*

            int rowPlayer = gameInfo.World.getNumRow(player.Position.Y);
            int columnPlayer = gameInfo.World.getNumColumn(player.Position.X);
            int previousRow = gameInfo.World.getNumRow(previousPosition.Y);
            int previousColumn = gameInfo.World.getNumColumn(previousPosition.X);
            //if (rowPlayer != previousRow || columnPlayer != previousColumn)
            //{
                Cell cell = gameInfo.World.get(rowPlayer, columnPlayer);
                if (cell.Obj != null)
                {
                    // Colision
                    player.Position.X = previousPosition.X;
                    player.Position.Y = previousPosition.Y;
                    System.Console.ReadKey();
                    EventEmitter.getInstance().registerMessage(0, aiPlayer.Id, aiPlayer.Id, Message.MessageType.msg_stop);
                }
                else
                {
                    cell = gameInfo.World.get(previousRow, previousColumn);
                    cell.Obj = null;
                    cell = gameInfo.World.get(rowPlayer, columnPlayer);
                    cell.Obj = player;
                }

            //}
            */

        }

        public override void exit(AIObject aiObject)
        {
            //Console.WriteLine("<Player_MoveState> exit");
            AIPlayer aiPlayer = (AIPlayer)aiObject;
            Player player = (Player)aiPlayer.Player;
            player.Moving = false;
        }

        public override Boolean onMessage(AIObject aiObject, Message message)
        {
            //Console.WriteLine("<Player_MoveState> onMessage " + message.Type);
            Boolean correct = false;
            switch (message.Type)
            {
                case Message.MessageType.msg_stop:
                    {
                        aiObject.StateMachine.changeState(Player_StopState.getInstance());
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_stopApproximate:
                    {
                        aiObject.StateMachine.changeState(Player_ApproximateState.getInstance());
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_attacked:
                    {
                        aiObject.StateMachine.changeState(Player_DestroyState.getInstance());
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_disableBlinking:
                    {
                        AIPlayer aiPlayer = (AIPlayer)aiObject;
                        Player player = (Player)aiPlayer.Player;
                        player.Blinking = false;
                        correct = true;
                        break;
                    }
                case Message.MessageType.msg_newShoot:
                    {
                        AIPlayer aiPlayer = (AIPlayer)aiObject;
                        aiPlayer.Player.Attack = true;
                        correct = true;
                        break;
                    }
            }
            return correct;
        }
    }
}
